// 处理用户登录的action
// import axios from 'axios'
import * as types from '../actionType'
import http from '@/utils/http.js'

import { setToken as setTokenLocal, clearToken } from '@/utils/auth'
export const setToken = (payload) => ({ type: types.SET_TOKEN, payload })
export const updateUserInfo = (payload) => ({ type: types.UPDATE_USERINFO, payload })

export const loginAction = (data) => {
    return async (dispatch) => {
        // 发送异步请求
        const { data: { token } } = await http.post("/v1_0/authorizations", data)
        // localStorage.setItem('geek-pc-19', token)
        setTokenLocal(token)
        dispatch(setToken(token))
    }
}


export const getUserInfo = () => {
    return async (dispatch) => {
        const { data } = await http.get('/v1_0/user/profile')
        dispatch(updateUserInfo(data))
    }
}

export const logoutAction = () => {
    return (dispatch) => {
        clearToken()
        dispatch(setToken(""))
        dispatch(updateUserInfo({}))
    }
}